package net.angrynut.web.handler.controller;

import javax.annotation.Resource;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.convert.ConversionService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import net.angrynut.web.model.dto.LoginForm;
import net.angrynut.web.model.entity.User;

/**
 * 演示 Spring 类型转换。
 * 
 * <ol>
 * <li>编写一个类型转换器。本例中是
 * {@link net.angrynut.web.model.converter.LoginFormToUserConverter
 * LoginFormToUserConverter}</li>
 * <li>注册编写的类型转换器。本例中的类型转换器注册在
 * {@link net.angrynut.web.configuration.WebBasicConfiguration#addFormatters
 * WebBasicConfiguration.addFormatters} 中</li>
 * <li>进行类型转换。使用注入进来的 {@link ConversionService}，调用其 {@code convert} 方法。</li>
 * </ol>
 */
@RestController
@Slf4j
public class DemoConverterController {

	@Resource
	private ConversionService conversionService;

	@PostMapping("/demo/converter")
	public User converter(@RequestBody LoginForm form) {
		User user = conversionService.convert(form, User.class);
		log.info("convert {} to {}: ", LoginForm.class.getName(), User.class.getName(), user);
		return user;
	}
}
